#!/bin/sh
#
# This test uses three pools, three tapes, and
#  an autochanger. Note, the Director has four Pools in its
#  conf:  Default, Full, Inc, and Scratch.  Default is used in the
#  NightlySave job by default.  What is backed up is what
#  is in ${cwd}/tmp/file-list.
#
#  Note, we use the virtual disk autochanger.
#
TestName="three-pool-recycle-test"
JobName=Three-pool-recycle
. scripts/functions

cwd=`pwd`
scripts/cleanup
scripts/copy-2disk-confs
scripts/prepare-disk-changer

# remove Client Run Before Job
cp ${cwd}/bin/susan-dir.conf ${cwd}/tmp/1
sed "s%Client Run Before Job%#Client Run Before Job%" ${cwd}/tmp/1 >${cwd}/bin/susan-dir.conf


# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

# Write out bconsole commands to a file
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
@#setdebug level=150 storage=tape
label storage=tape volume=TestVolume001 slot=1 pool=Full drive=0
label storage=tape volume=TestVolume002 slot=2 pool=Default drive=0
label storage=tape volume=TestVolume003 slot=3 pool=Scratch drive=0
update Volume=TestVolume001 maxvoljobs=1
update Volume=TestVolume002 maxvoljobs=1
update Volume=TestVolume003 volretention=7s
update Volume=TestVolume003 maxvoljobs=1
list volumes
llist volume=TestVolume003
@# should pull Scratch volume
run job=NightlySave1 level=Full pool=Inc yes
wait
update Volume=TestVolume003 volretention=7s
update Volume=TestVolume003 maxvoljobs=1
messages
list volumes
llist volume=TestVolume003
run job=NightlySave1 level=Full pool=Default yes
wait
messages
list volumes
llist volume=TestVolume003
run job=NightlySave1 level=Full pool=Full yes
wait
@sleep 10
messages
list volumes
llist volume=TestVolume003
@# should recycle Scratch pool
run job=NightlySave2 level=Full pool=Inc yes
wait
update Volume=TestVolume003 volretention=7s
update Volume=TestVolume003 maxvoljobs=1
@sleep 10
messages
list volumes
@# should recycle Scratch pool
llist volume=TestVolume003
@#setdebug level=50 dir
run job=NightlySave2 level=Full pool=Default yes
wait
update Volume=TestVolume003 volretention=7s
update Volume=TestVolume003 maxvoljobs=1
@sleep 10
messages
list volumes
@# should recycle Scratch pool
run job=NightlySave2 level=Full pool=Full yes
status storage=tape
messages
wait
list volumes
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/susan-restores select all storage=tape done
yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=tape
stop_susan

check_two_logs
check_restore_diff
end_test
